/*
** Copyright (C) 2015-2016  Michael Roland <mi.roland@fh.hagenberg.at>
**                          FH OÖ Forschungs & Entwicklungs GmbH
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/
package org.simalliance.openmobileapi.service.terminals.exploit.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.simalliance.openmobileapi.service.terminals.exploit.ContextInfo;
import org.simalliance.openmobileapi.service.terminals.exploit.OmapiInfo;
import org.simalliance.openmobileapi.service.terminals.exploit.R;

public class MainActivity extends Activity {
    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    
    public static final String EXTRA_INFO_CONTEXT_CONSTRUCTOR = "info_context_constructor";
    public static final String EXTRA_INFO_CONTEXT_CALLBACK = "info_context_callback";
    public static final String EXTRA_INFO_OMAPI = "info_omapi";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        View buttonShowCollected = findViewById(R.id.main_menu_action_showcollected);

        SharedPreferences prefs = getPreferences(MODE_PRIVATE);

        if (prefs.contains(EXTRA_INFO_CONTEXT_CONSTRUCTOR) ||
            getIntent().hasExtra(EXTRA_INFO_CONTEXT_CONSTRUCTOR)) {
            buttonShowCollected.setVisibility(View.VISIBLE);
        } else {
            buttonShowCollected.setVisibility(View.INVISIBLE);
        }

        if (getIntent().hasExtra(EXTRA_INFO_CONTEXT_CONSTRUCTOR)) {
            ContextInfo infoConstructor = (ContextInfo)getIntent().getSerializableExtra(EXTRA_INFO_CONTEXT_CONSTRUCTOR);
            prefs.edit().putString(EXTRA_INFO_CONTEXT_CONSTRUCTOR, infoConstructor.toString()).apply();
        }

        if (getIntent().hasExtra(EXTRA_INFO_CONTEXT_CALLBACK)) {
            ContextInfo infoConstructor = (ContextInfo)getIntent().getSerializableExtra(EXTRA_INFO_CONTEXT_CALLBACK);
            prefs.edit().putString(EXTRA_INFO_CONTEXT_CALLBACK, infoConstructor.toString()).apply();
        }

        if (getIntent().hasExtra(EXTRA_INFO_OMAPI)) {
            OmapiInfo infoConstructor = (OmapiInfo)getIntent().getSerializableExtra(EXTRA_INFO_OMAPI);
            prefs.edit().putString(EXTRA_INFO_OMAPI, infoConstructor.toString()).apply();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
            case R.id.menu_about:
                this.startActivity(new Intent(this, AboutActivity.class));
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void menuButton_Clicked(View v) {
        if (v != null) {
            switch (v.getId()) {
                case R.id.main_menu_action_showcontext:
                    {
                        ContextInfo contextInfo = new ContextInfo(this);

                        startActivity(
                                new Intent(this, ViewerActivity.class)
                                        .putExtra(ViewerActivity.EXTRA_VIEWDATANAME, "activityctx")
                                        .putExtra(ViewerActivity.EXTRA_VIEWDATA, contextInfo.toString()));
                    }
                    break;
                    
                case R.id.main_menu_action_showcollected:
                    {
                        Intent intent = getIntent();
                        ContextInfo infoConstructor = (ContextInfo)intent.getSerializableExtra(EXTRA_INFO_CONTEXT_CONSTRUCTOR);
                        ContextInfo infoCallback = (ContextInfo)intent.getSerializableExtra(EXTRA_INFO_CONTEXT_CALLBACK);
                        OmapiInfo infoOmapi = (OmapiInfo)intent.getSerializableExtra(EXTRA_INFO_OMAPI);

                        String dataToView = null;

                        if (infoConstructor != null) {
                            ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
                            try {
                                PrintStream printStream = new PrintStream(byteOutStream, false, "UTF-8");
                                infoConstructor.dump(printStream);
                                if (infoCallback != null) {
                                    infoCallback.dump(printStream);
                                }
                                if (infoOmapi != null) {
                                    infoOmapi.dump(printStream);
                                }
                                printStream.flush();
                                printStream.close();
                                dataToView = byteOutStream.toString("UTF-8");
                            } catch (Exception e) {
                                Log.e(LOG_TAG, "Could not process data", e);
                                Toast toast = Toast.makeText(this, R.string.processing_failed, Toast.LENGTH_SHORT);
                                toast.show();
                            }
                        } else {
                            SharedPreferences prefs = getPreferences(MODE_PRIVATE);
                            StringBuilder sb = new StringBuilder();
                            sb.append(prefs.getString(EXTRA_INFO_CONTEXT_CONSTRUCTOR, ""));
                            sb.append(prefs.getString(EXTRA_INFO_CONTEXT_CALLBACK, ""));
                            sb.append(prefs.getString(EXTRA_INFO_OMAPI, ""));
                            dataToView = sb.toString();
                        }

                        if (dataToView != null) {
                            startActivity(
                                    new Intent(this, ViewerActivity.class)
                                            .putExtra(ViewerActivity.EXTRA_VIEWDATANAME, "collectedctx")
                                            .putExtra(ViewerActivity.EXTRA_VIEWDATA, dataToView));
                        }
                    }
                    break;
            }
        }
    }
}
